package com.ease.framework.gef4p.commands {
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.editparts.NodeEditPart;
   import com.ease.framework.gef4p.editparts.PoolEditPart;
   import com.ease.framework.gef4p.model.Edge;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.gef4p.model.Pool;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;
   import com.ease.framework.workbench.spi.impls.CompoundCommand;

   public class RemoveNodeCommand extends CompoundCommand {
      public var pool:Pool;

      public var activity:Node;

      public var actEP:NodeEditPart;

      public function RemoveNodeCommand(act:Node) {
         super();
         activity = act;
         pool = activity.pool;

         for each (var tran:Edge in activity.geInEdges()) {
            var removeEdgeCommand:ICommand = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(tran).createRemoveCommand();
            this.addCommand(removeEdgeCommand);
         }
         for each (tran in activity.geOutEdges()) {
            removeEdgeCommand = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(tran).createRemoveCommand();
            this.addCommand(removeEdgeCommand);
         }

      }

      override public function execute():void {
         super.execute();
         pool.removeNode(activity);
         if (actEP == null) {
            actEP = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(activity) as NodeEditPart;
         }
         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(pool) as PoolEditPart;
         processEditPart.removeNodeEditPart(actEP);
         actEP.remove();
      }

      override public function undo():void {
         pool.addNode(activity);
         actEP.unremove();
         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(pool) as PoolEditPart;
         processEditPart.addNodeEditPart(actEP);
         super.undo();
      }
   }
}